package mapper

import (
	"GinShop/config"
	"GinShop/model"
	"GinShop/utils"
)

type MemberMapper struct {
	*config.Orm
}

func NewMemberMapper() *MemberMapper {
	return &MemberMapper{config.DB}
}

func (md *MemberMapper) QueryMemberById(id int64) *model.Member {
	var member model.Member
	md.Where(" id = ? ", id).First(&member)

	return &member
}

func (md *MemberMapper) InsertCode(sms model.SmsCode) int64 {
	result := md.Create(&sms)

	return result.RowsAffected
}

//根据用户名和密码查询
func (md *MemberMapper) Query(name string, password string) *model.Member {
	var member model.Member
	password = utils.EncoderSha256(password)
	md.Where(" user_name = ? AND password = ? ", name, password).First(&member)

	return &member
}

//验证手机号和验证码是否存在
func (md *MemberMapper) ValidateSmsCode(phone string, code string) *model.SmsCode {
	var sms model.SmsCode
	md.Where(" phone = ? AND code = ? ", phone, code).First(&sms)

	return &sms
}

func (md *MemberMapper) QueryByPhone(phone string) *model.Member {
	var member model.Member
	md.Where(" mobile  = ? ", phone).First(&member)

	return &member
}

//新用户的数据库插入操作
func (md *MemberMapper) InsertMember(member model.Member) int64 {
	result := md.Create(&member)

	return result.RowsAffected
}

//更新member记录，头像属性
func (md *MemberMapper) UpdateMemberAvatar(userId int64, fileName string) int64 {
	result := md.Model(&model.Member{}).Where(" id = ? ", userId).Update("avatar", fileName)

	return result.RowsAffected
}
